package com.taichuan.selfcheck.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.taichuan.keysupport.Key;
import com.taichuan.mvp.fragment.BaseFragmentManager;
import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.config.PropertyDefaultValue;
import com.taichuan.selfcheck.entity.TestResultEntity;
import com.taichuan.selfcheck.mvp.presenter.MainPresenter;
import com.taichuan.selfcheck.mvp.viewinterface.MainInterface;
import com.taichuan.selfcheck.ui.activity.MainActivity;
import com.taichuan.selfcheck.ui.fragment.base.AppBaseFragment;
import com.taichuan.tcutils.taichuan.SystemUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2019/12/4
 */
public class MainFragment extends AppBaseFragment<MainInterface, MainPresenter> implements MainInterface {

    @BindView(R.id.check_result_button)
    Button checkResultBtn;
    @BindView(R.id.auto_check_button)
    Button autoCheckBtn;
    @BindView(R.id.screen_color_button)
    Button screenColorBtn;
    @BindView(R.id.screen_touch_button)
    Button screenTouchBtn;
    @BindView(R.id.micro_phone_button)
    Button microPhoneBtn;
    @BindView(R.id.camera_button)
    Button cameraBtn;
    @BindView(R.id.device_test_button)
    Button deviceIOBtn;
    @BindView(R.id.wifi_button)
    Button wifiBtn;
    @BindView(R.id.swipe_card_button)
    Button swipeCardBtn;
    @BindView(R.id.test_light_button)
    Button lighrBtn;
    @BindView(R.id.power_log_button)
    Button powerLogBtn;


    private BaseFragmentManager activityBaseFragmentManager;
    private MainActivity mMainActivity;

    @Override
    protected void getBundleValues(Bundle bundle) {

    }

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_main;
    }

    @Override
    protected void initView(View view, Bundle bundle) {
        activityBaseFragmentManager = getActivityBaseFragmentManager();

    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mMainActivity = (MainActivity) context;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden){
            //隐藏MainActivity 中的返回按钮
//            showOrHideMainBottomButton(false);
        }else {
            //显示MainActivity 中的返回按钮
            showTitle(R.string.settings_function_test);
        }
    }

    @OnClick({R.id.check_result_button, R.id.auto_check_button, R.id.screen_color_button,
            R.id.screen_touch_button, R.id.micro_phone_button, R.id.camera_button,
            R.id.device_test_button, R.id.wifi_button, R.id.swipe_card_button,
            R.id.test_light_button, R.id.power_log_button})
    @Override
    protected void onXmlClick(View v) {
        super.onXmlClick(v);
        switch (v.getId()) {
            case R.id.screen_color_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.screencolor);
                    activityBaseFragmentManager.replace(new SelectScreenTestFragment());
                }
                break;
            }
            case R.id.screen_touch_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.screentouch);
                    activityBaseFragmentManager.replace(new ScreenTouchFragment());
                }
                break;
            }
            case R.id.micro_phone_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.microphone);
                    activityBaseFragmentManager.replace(new TestAudioFragment());
                }
                break;
            }
            case R.id.camera_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.camera);
                    if(SystemUtils.isRk3399()){
                        activityBaseFragmentManager.replace(new SelectCameraFragment());
                    }else {
                        TestCameraFragment testCameraFragment = new TestCameraFragment();
                        testCameraFragment.setArguments(TestCameraFragment.setBundleValue(TestCameraFragment.CAMERA_TYPE_COLOR));
                        activityBaseFragmentManager.replace(testCameraFragment);
                    }
                }
                break;
            }
            case R.id.device_test_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.device_test);
                    activityBaseFragmentManager.replace(new IOStateFragment());
                }
                break;
            }
            case R.id.wifi_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.wifi);
                    activityBaseFragmentManager.replace(new WiFiFragment());
                }
                break;
            }
            case R.id.swipe_card_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.swipecard);
                    activityBaseFragmentManager.replace(new CardFragment());
                }
                break;
            }
            case R.id.test_light_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.light_test);
                    activityBaseFragmentManager.replace(new LightTestFragment());
                }
                break;
            }
            case R.id.power_log_button:{
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.power_log);
                    activityBaseFragmentManager.replace(new PowerOnOffFragment());
                }
                break;
            }
            case R.id.check_result_button: {
                if (activityBaseFragmentManager != null) {
                    showTitle(R.string.check_result);
                    activityBaseFragmentManager.replace(new TestResultFragment());
                }
                break;
            }
            case R.id.auto_check_button: {
                EventBus.getDefault().post(new TestResultEntity(0,getResources().getString(R.string.auto_check), PropertyDefaultValue.FUNCTION_TEST_RESULT));
                break;
            }
        }
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        switch (event.getKeyCode()){
            case KeyEvent.KEYCODE_1:        //按键1——屏幕颜色
                screenColorBtn.performClick();
                break;
            case KeyEvent.KEYCODE_2:        //按键2——触屏测试
//                screenTouchBtn.performClick();
                //TODO 默认设置按键板不进入触屏测试
                break;
            case KeyEvent.KEYCODE_3:        //按键3——麦克风测试
                microPhoneBtn.performClick();
                break;
            case KeyEvent.KEYCODE_4:        //按键4——摄像头测试
                cameraBtn.performClick();
                break;
            case KeyEvent.KEYCODE_5:        //按键5——门检防拆微波开锁测试
                deviceIOBtn.performClick();
                break;
            case KeyEvent.KEYCODE_6:        //按键6——WIFI测试
                wifiBtn.performClick();
                break;
            case KeyEvent.KEYCODE_7:        //按键7——刷卡测试
                swipeCardBtn.performClick();
                break;
            case KeyEvent.KEYCODE_8:        //按键8——灯光测试
                lighrBtn.performClick();
                break;
            case KeyEvent.KEYCODE_9:        //按键9——开关机日志
                powerLogBtn.performClick();
                break;
            case Key.Convert.KEYCODE_JING:     //按键#——测试报告
                checkResultBtn.performClick();
                break;
            case Key.Convert.KEYCODE_LONG_JING:    //长按按键#——自动测试
                EventBus.getDefault().post(new TestResultEntity(0,getResources().getString(R.string.auto_check), PropertyDefaultValue.FUNCTION_TEST_RESULT));
                break;
        }
        return super.dispatchKeyEvent(event);
    }

    /**
     * 定义一个方法用于 显示标题
     */
    private void showTitle(int title) {
        if (mMainActivity != null) {
//            if (mMainActivity.isStartAppByThree()){
//
//                mMainActivity.showToolBarTitle(title);
//            }
            mMainActivity.showToolBarTitle(title);
        }
    }


    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter();
    }

    @Override
    protected boolean isAllowAutoFinishFragment() {
        return false;
    }

    @Override
    public void doOnAutoFinishFragment() {

    }
}
